#ifndef __RS485_H
#define __RS485_H

#include "./SYSTEM/sys/sys.h"

/* -------------------------------------------- RS485引脚定义 ------------------------------------------- */

/* RS485_1 (USART1) 引脚定义 */
#define RS485_1_RE_GPIO_PORT                 GPIOA
#define RS485_1_RE_GPIO_PIN                  GPIO_PIN_8
#define RS485_1_RE_GPIO_CLK_ENABLE()         do{ __HAL_RCC_GPIOA_CLK_ENABLE(); }while(0)   /* PA口时钟使能 */

#define RS485_1_TX_GPIO_PORT                 GPIOA
#define RS485_1_TX_GPIO_PIN                  GPIO_PIN_9
#define RS485_1_TX_GPIO_CLK_ENABLE()         do{ __HAL_RCC_GPIOA_CLK_ENABLE(); }while(0)   /* PA口时钟使能 */

#define RS485_1_RX_GPIO_PORT                 GPIOA
#define RS485_1_RX_GPIO_PIN                  GPIO_PIN_10
#define RS485_1_RX_GPIO_CLK_ENABLE()         do{ __HAL_RCC_GPIOA_CLK_ENABLE(); }while(0)   /* PA口时钟使能 */

/* RS485_2 (USART3) 引脚定义 */
#define RS485_2_RE_GPIO_PORT                 GPIOE
#define RS485_2_RE_GPIO_PIN                  GPIO_PIN_15
#define RS485_2_RE_GPIO_CLK_ENABLE()         do{ __HAL_RCC_GPIOE_CLK_ENABLE(); }while(0)   /* PE口时钟使能 */

#define RS485_2_TX_GPIO_PORT                 GPIOB
#define RS485_2_TX_GPIO_PIN                  GPIO_PIN_10
#define RS485_2_TX_GPIO_CLK_ENABLE()         do{ __HAL_RCC_GPIOB_CLK_ENABLE(); }while(0)   /* PB口时钟使能 */

#define RS485_2_RX_GPIO_PORT                 GPIOB
#define RS485_2_RX_GPIO_PIN                  GPIO_PIN_11
#define RS485_2_RX_GPIO_CLK_ENABLE()         do{ __HAL_RCC_GPIOB_CLK_ENABLE(); }while(0)   /* PB口时钟使能 */

/* RS485_3 (USART2) 引脚定义 */
#define RS485_3_RE_GPIO_PORT                 GPIOD
#define RS485_3_RE_GPIO_PIN                  GPIO_PIN_4
#define RS485_3_RE_GPIO_CLK_ENABLE()         do{ __HAL_RCC_GPIOD_CLK_ENABLE(); }while(0)   /* PD口时钟使能 */

#define RS485_3_TX_GPIO_PORT                 GPIOD
#define RS485_3_TX_GPIO_PIN                  GPIO_PIN_5
#define RS485_3_TX_GPIO_CLK_ENABLE()         do{ __HAL_RCC_GPIOD_CLK_ENABLE(); }while(0)   /* PD口时钟使能 */

#define RS485_3_RX_GPIO_PORT                 GPIOD
#define RS485_3_RX_GPIO_PIN                  GPIO_PIN_6
#define RS485_3_RX_GPIO_CLK_ENABLE()         do{ __HAL_RCC_GPIOD_CLK_ENABLE(); }while(0)   /* PD口时钟使能 */

/* -------------------------------------------- RS485串口定义 ------------------------------------------- */

/* RS485_1 串口定义 */
#define RS485_1_UX                           USART1
#define RS485_1_UX_IRQn                      USART1_IRQn
#define RS485_1_UX_IRQHandler                USART1_IRQHandler
#define RS485_1_UX_CLK_ENABLE()              do{ __HAL_RCC_USART1_CLK_ENABLE(); }while(0)  /* USART1 时钟使能 */

/* RS485_2 串口定义 */
#define RS485_2_UX                           USART3
#define RS485_2_UX_IRQn                      USART3_IRQn
#define RS485_2_UX_IRQHandler                USART3_IRQHandler
#define RS485_2_UX_CLK_ENABLE()              do{ __HAL_RCC_USART3_CLK_ENABLE(); }while(0)  /* USART3 时钟使能 */

/* RS485_3 串口定义 */
#define RS485_3_UX                           USART2
#define RS485_3_UX_IRQn                      USART2_IRQn
#define RS485_3_UX_IRQHandler                USART2_IRQHandler
#define RS485_3_UX_CLK_ENABLE()              do{ __HAL_RCC_USART2_CLK_ENABLE(); }while(0)  /* USART2 时钟使能 */

/* -------------------------------------------- RS485控制定义 ------------------------------------------- */

/* RS485发送/接收控制宏，RS485_RE = 0, 进入接收模式，RS485_RE = 1, 进入发送模式 */
#define RS485_1_RE(x)  do{ x ? \
                         HAL_GPIO_WritePin(RS485_1_RE_GPIO_PORT, RS485_1_RE_GPIO_PIN, GPIO_PIN_SET) : \
                         HAL_GPIO_WritePin(RS485_1_RE_GPIO_PORT, RS485_1_RE_GPIO_PIN, GPIO_PIN_RESET); \
                     }while(0)

#define RS485_2_RE(x)  do{ x ? \
                         HAL_GPIO_WritePin(RS485_2_RE_GPIO_PORT, RS485_2_RE_GPIO_PIN, GPIO_PIN_SET) : \
                         HAL_GPIO_WritePin(RS485_2_RE_GPIO_PORT, RS485_2_RE_GPIO_PIN, GPIO_PIN_RESET); \
                     }while(0)

#define RS485_3_RE(x)  do{ x ? \
                         HAL_GPIO_WritePin(RS485_3_RE_GPIO_PORT, RS485_3_RE_GPIO_PIN, GPIO_PIN_SET) : \
                         HAL_GPIO_WritePin(RS485_3_RE_GPIO_PORT, RS485_3_RE_GPIO_PIN, GPIO_PIN_RESET); \
                     }while(0)

/* -------------------------------------------- RS485参数定义 ------------------------------------------- */

#define RS485_REC_LEN               64          /* 定义最大接收字节数 64 */
#define RS485_EN_RX                 1           /* 使能（1）/禁止（0）RS485接收 */

/* --------------------------------------------- 全局变量声明 ------------------------------------------- */

/* RS485_1 变量 */
extern UART_HandleTypeDef g_rs485_1_handler;    /* RS485_1 控制句柄 */
extern uint8_t g_RS485_1_rx_buf[RS485_REC_LEN]; /* 接收缓冲,最大RS485_REC_LEN个字节 */
extern uint8_t g_RS485_1_rx_cnt;                /* 接收数据长度 */

/* RS485_2 变量 */
extern UART_HandleTypeDef g_rs485_2_handler;    /* RS485_2 控制句柄 */
extern uint8_t g_RS485_2_rx_buf[RS485_REC_LEN]; /* 接收缓冲,最大RS485_REC_LEN个字节 */
extern uint8_t g_RS485_2_rx_cnt;                /* 接收数据长度 */

/* RS485_3 变量 */
extern UART_HandleTypeDef g_rs485_3_handler;    /* RS485_3 控制句柄 */
extern uint8_t g_RS485_3_rx_buf[RS485_REC_LEN]; /* 接收缓冲,最大RS485_REC_LEN个字节 */
extern uint8_t g_RS485_3_rx_cnt;                /* 接收数据长度 */

/* ----------------------------------------------- 函数声明 --------------------------------------------- */

/* RS485_1 函数 */
void rs485_1_init(uint32_t baudrate);                  /* RS485_1初始化 */
void rs485_1_send_data(uint8_t *buf, uint8_t len);     /* RS485_1发送数据 */
void rs485_1_receive_data(uint8_t *buf, uint8_t *len); /* RS485_1接收数据 */

/* RS485_2 函数 */
void rs485_2_init(uint32_t baudrate);                  /* RS485_2初始化 */
void rs485_2_send_data(uint8_t *buf, uint8_t len);     /* RS485_2发送数据 */
void rs485_2_receive_data(uint8_t *buf, uint8_t *len); /* RS485_2接收数据 */

/* RS485_3 函数 */
void rs485_3_init(uint32_t baudrate);                  /* RS485_3初始化 */
void rs485_3_send_data(uint8_t *buf, uint8_t len);     /* RS485_3发送数据 */
void rs485_3_receive_data(uint8_t *buf, uint8_t *len); /* RS485_3接收数据 */

#endif
